package net.oschina.app.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;

import net.oschina.app.adapter.TweetNewAdapter;
import net.oschina.app.base.BaseAdapter;
import net.oschina.app.base.BaseFragment;
import net.oschina.app.bean.DetailTweetBean;
import net.oschina.app.bean.MoveNewBean;
import net.oschina.app.bean.TweetBean;
import net.oschina.app.protocol.DetailTweetProtocol;
import net.oschina.app.protocol.TweetNewProtocol;
import net.oschina.app.ui.TweetActivity;
import net.oschina.app.util.CommonUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/1/13.
 * 最新动弹
 */

public class TweetNewFragment extends BaseFragment {
    private TweetNewAdapter mTweetNewAdapter;
    private List<TweetBean> mTweets;

    @Override
    protected void OnSubItemClick(RecyclerAdapterWithHF adapter, RecyclerView.ViewHolder vh, int position) {
        super.OnSubItemClick(adapter, vh, position);
        final Intent intent = new Intent(getContext(), TweetActivity.class);
        TweetBean tweetBean = mTweets.get(position);
        final int id = tweetBean.getId();
        new Thread(new Runnable() {
            @Override
            public void run() {
                DetailTweetProtocol detailTweetProtocol = new DetailTweetProtocol(id);
                DetailTweetBean detailTweetBean = detailTweetProtocol.getData(0);
                Bundle bundle = new Bundle();
                bundle.putSerializable("bean",detailTweetBean.getTweet());
                intent.putExtras(bundle);
                intent.putExtra("title", "资讯详情");
                startActivity(intent);
            }
        }).start();
    }

    @Override
    protected BaseAdapter getRvAdapter() {
        mTweetNewAdapter = new TweetNewAdapter(getContext());
        return mTweetNewAdapter;
    }

    @Override
    protected int onLoadSubData() {
        Bundle arguments = getArguments();
        String uid = "";
        if (arguments != null) {
            uid = arguments.getString("uid");
        }
        TweetNewProtocol tweetNewProtocol = new TweetNewProtocol(uid);
        tweetNewProtocol.setPageIndex(mPageIndex);
        final MoveNewBean moveNewBean = tweetNewProtocol.getData(mList.size()/20);
        if (moveNewBean != null) {
            mPageIndex++;
            mTweets = moveNewBean.getTweets();
            if (mTweets.size() == 0 && mPageIndex == 0) {            //没有数据
                return STATE_EMPTY;
            }
            mList.addAll(mTweets);
            CommonUtils.runOnMainThread(new Runnable() {
                @Override
                public void run() {
                    if (mTweets.size() == 0) {
                        Toast.makeText(getContext(), "没有更多数据", Toast.LENGTH_SHORT).show();
                        ptrClassicFrameLayout.setLoadMoreEnable(false);
                    }
                    mTweetNewAdapter.notifyDataSetChanged();
                }
            });
            return STATE_SUCCESS;
        }
        return STATE_ERROR;
    }
}
